<?php

class csMySql {
	public $dbh;
	public $default_fetchmode;
	public $debug_mode = false;

	public function __construct($params = null) {		
		$this -> default_fetchmode = MYSQL_ASSOC;
		if (isset($params['debug_mode']))
			$this -> debug_mode = $params['debug_mode'];
	}

	public function connect($params) {
		if (! isset($params['db_host']) || ! $params['db_host'] || ! isset($params['db_user']) || ! $params['db_user'] || ! isset($params['db_pass']) || ! $params['db_pass']) {
			throw new csMysqlException(translate('Could not connect to database'), 888005);
		}
		
		$this -> dbh = mysql_connect($params['db_host'], $params['db_user'], $params['db_pass']);
		
		if (! $this -> dbh) {
			throw new csMysqlException(translate('Could not connect to database'), 888006);
		}
	}

	public function select_db($db_name) {
		if (! $db_name || ! $this -> dbh) {
			throw new csMysqlException(translate('Could not select database'), 888006);
		}
		
		if (! mysql_select_db($db_name, $this -> dbh)) {
			throw new csMysqlException(translate('Could not select database'), 888007);
		}
	}

	public function query($query) {
		if (! $this -> dbh)
			throw new csMysqlException(translate('Null DB handle'), 888008);
		if ($this -> debug_mode) {
			$tstart = time() + microtime();
			$sth = mysql_query($query, $this -> dbh);
			$tend = time() + microtime();
			dquery($query . " (EXEC_TIME: " . round(($tend - $tstart), 4) . "s)");
		} else {
			$sth = mysql_query($query, $this -> dbh);
		}
		if (! $sth)
			throw new csMysqlException(translate('Wrong query'), 888009, $query);
		
		return $sth;
	}

	public function query_col($query, $col) {
		$sth = $this -> query($query);
		
		$num_rows = mysql_num_rows($sth);
		
		if ($num_rows > 1)
			throw new csMysqlException(translate('Index of this query is not unique'), 888010, $query);
		if ($num_rows == 0)
			throw new csMysqlException(translate('Null result of this query'), 888011, $query);
		
		if (is_integer($col)) {
			$row = mysql_fetch_row($sth);
		} else {
			$row = mysql_fetch_assoc($sth);
		}
		
		return $row[$col];
	}

	public function query_row($query, $fetchmode = null) {
		$sth = $this -> query($query);
		
		$num_rows = mysql_num_rows($sth);
		
		if ($num_rows > 1)
			throw new csMysqlException(translate('Index of this query is not unique'), 888012, $query);
		
		$row = $this -> fetch_array($sth, $fetchmode);
		
		return $row;
	}

	public function query_row_tbl($query, $tab_index = null, $fetchmode = null) {
		$sth = $this -> query($query);
		$result_tbl = array(
		);
		while ($row = $this -> fetch_array($sth, $fetchmode)) {
			if ($tab_index !== null && isset($row[$tab_index]) && ! isset($result_tbl[$row[$tab_index]])) {
				$result_tbl[$row[$tab_index]] = $row;
			} else {
				$result_tbl[] = $row;
			}
		}
		return $result_tbl;
	}

	public function query_row_tbl_subst($query, $subst, $tab_index = null, $fetchmode = null) {
		$query = $this -> prepare_query($query, $subst);
		return $this -> query_row_tbl($query, $tab_index, $fetchmode);
	}

	public function query_subst($query, $subst) {
		$query = $this -> prepare_query($query, $subst);
		
		return $this -> query($query);
	}

	public function query_col_subst($query, $subst, $col) {
		$query = $this -> prepare_query($query, $subst);
		
		return $this -> query_col($query, $col);
	}

	public function query_row_subst($query, $subst, $fetchmode = null) {
		$query = $this -> prepare_query($query, $subst);
		
		return $this -> query_row($query, $fetchmode);
	}

	protected function prepare_query($query, $subst) {
		foreach ($subst as $patt => $val) {
			$val = $this -> quote($val);
			$query = str_replace("@@$patt@@", (string)$val, $query);
		}
		return $query;
	}

	public function quote($value, $dont_quote = false) {
		$value = (get_magic_quotes_gpc()) ? (stripslashes($value)) : ($value);
		if ($value === null) {
			$value = 'NULL';
		} else if (is_numeric($value)) {} else if (is_string($value)) {
			$value = mysql_real_escape_string($value, $this -> dbh);
		} else {
			throw new csErrorException(translate('Unhandled datatype in quote'), 888014);
		}
		return ($dont_quote ? $value : "'" . $value . "'");
	}

	public function affected_rows() {
		return mysql_affected_rows();
	}

	public function last_insert_id() {
		return $this -> query_col("SELECT LAST_INSERT_ID()", 0);
	}

	public function fetch_array($sth, $fetchmode = null) {
		return mysql_fetch_array($sth, ($fetchmode ? $fetchmode : $this -> default_fetchmode));
	}

	public function free($result) {
		return is_resource($result) ? mysql_free_result($result) : false;
	}
}

?>
